﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manager;
using Model;
using ExtAspNet;
using Manager.CustomerHelper;
namespace HealthSystem.sysAdmin
{
    public partial class OrgSingle_Add : BasePage
    {
        HW_OrgUserManager usermanager = new HW_OrgUserManager();
        public override ModuleType Module
        {
            get
            {
                return ModuleType.OrgManager;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHidePostBackReference();
                if (Request["act"] as string == "edit")
                {
                    cnnname.Text = usermanager.GetById(Request["id"]).CNName;
                }
            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                HW_OrgUser user = new HW_OrgUser();
                if (Request["act"] as string == "edit")
                {
                    user = usermanager.GetById(Request["id"] as string);
                    user.CNName = cnnname.Text;
                    usermanager.ModHW_OrgUser(user);
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
                else
                {
                    string orgcode = Request["orgcode"] as string;
                    string temp = usermanager.GetMaxUserName(orgcode);
                    if (string.IsNullOrEmpty(temp))
                        user.XX_UserName = orgcode;
                    else
                        if (temp.Length < 9)
                            user.XX_UserName = orgcode + "1";
                        else
                            user.XX_UserName = temp.Substring(0, 7) + (Convert.ToInt32(temp.Substring(7)) + 1).ToString();
                    user.CNName = cnnname.Text.Trim();
                    user.OrgCode = orgcode;
                    user.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    user.RoleId = 2000;
                    usermanager.Add(user);
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
            }
            catch
            {
                Alert.Show("添加机构管理员出错，请检查");
            }
        }
    }
}